FUNCTION YBC_SWS_CANCEL_REQUEST.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IV_REQUESTID) TYPE  YBC_SWS_REQUEST-REQUESTID OPTIONAL
*"     VALUE(IV_APPID) TYPE  YBC_SWS_REQUEST-APPID OPTIONAL
*"     VALUE(IV_BUSINESSID) TYPE  YBC_SWS_REQUEST-BUSINESSID OPTIONAL
*"     VALUE(IV_NO_EXIST_CHECK) TYPE  CHAR1 OPTIONAL
*"     VALUE(IV_NO_STATUS_CHECK) TYPE  CHAR1 OPTIONAL
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"----------------------------------------------------------------------
  DATA:
    LTR_REQUEST TYPE RANGE OF YBC_SWS_REQUEST-REQUESTID,
    LSR_REQUEST LIKE LINE OF LTR_REQUEST,
    LT_REQUEST  TYPE STANDARD TABLE OF YBC_SWS_REQUEST,
    LV_REMARKS  TYPE YBC_SWS_APPROVER-REMARKS.

  IF IV_REQUESTID IS INITIAL AND IV_APPID IS INITIAL AND IV_BUSINESSID IS INITIAL.
    ES_MSG-TYPE = 'E'.
    MESSAGE S012(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  IF IV_REQUESTID IS INITIAL AND ( IV_APPID IS INITIAL OR IV_BUSINESSID IS INITIAL ).
    ES_MSG-TYPE = 'E'.
    MESSAGE S012(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  IF IV_REQUESTID IS NOT INITIAL.
    SELECT  *
      INTO TABLE LT_REQUEST
      FROM YBC_SWS_REQUEST
     WHERE REQUESTID = IV_REQUESTID
       AND MOD = GC_MOD_NEW.

  ELSEIF IV_APPID IS NOT INITIAL AND IV_BUSINESSID IS NOT INITIAL.
    SELECT *
      INTO TABLE LT_REQUEST
      FROM YBC_SWS_REQUEST
     WHERE APPID = IV_APPID
       AND BUSINESSID = IV_BUSINESSID
       AND MOD = GC_MOD_NEW.
  ENDIF.
  "记录不存在也不会报错
  IF SY-SUBRC <> 0 AND IV_NO_EXIST_CHECK IS NOT INITIAL.
    RETURN.
  ENDIF.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S021(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  IF IV_NO_STATUS_CHECK IS INITIAL.
    LOOP AT LT_REQUEST INTO DATA(LS_REQUEST) WHERE STATUS <> GC_STATUS_SUBMITTED .
      EXIT.
    ENDLOOP.
    IF SY-SUBRC = 0.
      ES_MSG-TYPE = 'E'.
      MESSAGE S031(YBC_SWS) INTO ES_MSG-MESSAGE.
      RETURN.
    ENDIF.
  ENDIF.

  MESSAGE S013(YBC_SWS) WITH SY-UNAME INTO LV_REMARKS.
  LOOP AT LT_REQUEST INTO LS_REQUEST.
    LS_REQUEST-STATUS = GC_STATUS_CANCELLED.
    LS_REQUEST-MOD = GC_MOD_CANCEL.
    LS_REQUEST-MESSAGE = LV_REMARKS .

    MODIFY LT_REQUEST FROM  LS_REQUEST.

    CLEAR LSR_REQUEST.
    LSR_REQUEST-SIGN = 'I'.
    LSR_REQUEST-OPTION = 'EQ'.
    LSR_REQUEST-LOW = LS_REQUEST-REQUESTID.
    APPEND LSR_REQUEST TO LTR_REQUEST.
  ENDLOOP.

  MODIFY YBC_SWS_REQUEST FROM TABLE LT_REQUEST.

*  UPDATE YBC_SWS_APPROVER SET CANCELED = 'X'
*    WHERE REQUESTID IN LTR_REQUEST.

  "Remarks 更新，只更新还未处理的审批级别
*  UPDATE YBC_SWS_APPROVER SET CANCELED = 'X' REMARKS = LV_REMARKS
*    WHERE REQUESTID IN LTR_REQUEST
*      AND PROCESSED = SPACE.

  UPDATE YBC_SWS_APPROVER
    SET CANCELED = 'X'
        PROCESSED = 'X'
        REMARKS = LV_REMARKS
  WHERE REQUESTID IN LTR_REQUEST
    AND PROCESSED = SPACE.

  COMMIT WORK AND WAIT.
  IF SY-SUBRC <> 0.
    ROLLBACK WORK.
    RETURN.
  ENDIF.

  ES_MSG-TYPE = 'S'.
  MESSAGE S013(YBC_SWS) WITH SY-UNAME INTO ES_MSG-MESSAGE.
ENDFUNCTION.
